﻿/* mvcForum
 * Copyright (C) 2010-2011 Steen F. Tøttrup
 * http://mvcforum.org/
 */

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

using mvcForum.Core.Abstractions;
using mvcForum.Core.Abstractions.Interfaces;

namespace mvcForum.Core {

	public class ForumSettings {
		public ForumSettings() { }
		public ForumSettings(String key, Int32 value) : this(key, value.ToString()) { }
		public ForumSettings(String key, Boolean value) : this(key, value.ToString()) { }
		public ForumSettings(String key, String value) {
			this.Key = key;
			this.Value = value;
		}

		public virtual Int32? GetInt32() {
			Int32? output = null;
			Int32 tmp;
			if (Int32.TryParse(this.Value, out tmp)) {
				return tmp;
			}
			return output;
		}

		public virtual void SetInt32(Int32 value) {
			this.Value = value.ToString();
		}

		public virtual Boolean GetBoolean() {
			Boolean output = false;
			Boolean.TryParse(this.Value, out output);
			return output;
		}

		public virtual void SetBoolean(Boolean value) {
			this.Value = value.ToString();
		}

		public virtual IList<Int32> GetInt32List() {
			return this.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(v => Int32.Parse(v)).ToList();
		}

		public void SetInt32List(IList<Int32> values) {
			this.Value = "";
			foreach (Int32 value in values) {
				this.Value += String.Format("{0};", value);
			}
		}

		#region Properties
		public Int32 Id { get; set; }
		[StringLength(Constants.FieldLengths.SettingsKey)]
		[Required]
		public String Key { get; set; }
		[StringLength(Int32.MaxValue)]
		public String Value { get; set; }
		#endregion
	}
}